#include<stdio.h>
#include<stdlib.h>
#include"DoubleLinkList.h"
#define ture 1
#define false 0

int InitDLlist(DLlist *list)
{
    list->head = NULL;
    list->tail = NULL;
    list->len = 0;
    return 0;
}

struct Node* CreateNode(ElementType element)
{
    struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
    if(newNode == NULL)
    {
        printf("Create newNode error!\n");
        return NULL;
    }

    newNode->next = NULL;
    newNode->prev = NULL;
    newNode->data = element;
    return newNode;
}

void InsertTail(DLlist *list, ElementType element)
{
    struct Node* newNode = CreateNode(element);
    if(newNode == NULL)
    {
        printf("InsertTail create error!\n");
        return;
    }

    if(list->len == 0)
    {
        list->head = newNode;
        list->tail = newNode;
    }
    else
    {
        list->tail->next = newNode;
        newNode->prev = list->tail;
        list->tail = newNode;
    }
    list->len++;
}

void Travel(DLlist *list)
{

    struct Node *TravelPoint = list->head;
    printf("next:   ");
    while(TravelPoint != NULL)
    {
        //printf("%d ",TravelPoint->data);
        TravelPoint = TravelPoint->next;
    }
    printf("\n");

    printf("Prev:   ");
    TravelPoint = list->tail;
    while(TravelPoint != NULL)
    {
        //printf("%d ",TravelPoint->data);
        TravelPoint = TravelPoint->prev;
    }
    printf("\n");
}

void InsertHead(DLlist *list, ElementType element)
{
    struct Node* newNode = CreateNode(element);
    if(newNode == NULL)
    {
        printf("InsertHead create error!\n");
        return;
    }

    if(list->len == 0)
    {
        list->head = newNode;
        list->tail = newNode;
    }
    else
    {
        list->head->prev = newNode;
        newNode->next = list->head;
        list->head = newNode;
    }
    list->len++;
}

void InsertIndex(DLlist*list,ElementType element,int index)
{
    if(index < 0 || index >=list->len)
    {
        printf("insertIndex place error!!!\n");
        return;
    }

    struct Node *newNode = CreateNode(element);
    // printf("%d  ",newNode->data);
    struct Node *TravelPoint = list->head;
    if(index ==0)
    {
        newNode->next = list->head;
        list->head->prev = newNode;
        list->head = newNode;
        list->len++;
        return;
    }
    if(index == list->len-1)
    {
        list->tail->next = newNode;
        newNode->prev = list->tail;
        list->tail = newNode;
        list->len++;
        return;
    }

    while(index > 0)
    {
        index--;
        TravelPoint = TravelPoint->next;
    }
    
    // struct Node *temp = TravelPoint->prev;
    // newNode->next = TravelPoint;
    // TravelPoint->prev = newNode;
    // temp->next = newNode;
    // newNode->prev = temp;
    //printf("%d  ",TravelPoint->data);

    newNode->next = TravelPoint;
    newNode->prev = TravelPoint->prev;
    TravelPoint->prev->next = newNode;
    TravelPoint->prev = newNode;
    
    list->len++;
}

void RemoveByIndex(DLlist *list, int index)
{
    if(index < 0 || index >=list->len)
    {
        printf("RemoveByIndex invaild place!\n");
        return;
    }
    if(index ==0)   //当删除的元素前面为空时候
    {
        if(list->len ==1)     //当只有一个元素的时候
        {
            free(list->head);
            list->head=NULL;    //头结点置空
            list->tail=NULL;    //尾节点置空
            list->len=0;
            return;
        }
        struct Node *freeNode =list->head;  
        list->head=list->head->next;    //头结点往后动
        list->head->prev=NULL;         //前驱节点指向空
        free(freeNode);
        list->len--;
        return;
    }

    if(index == list->len-1)
    {
        struct Node *freeNode =list->tail;  
        list->tail=list->tail->prev;    
        list->tail->next=NULL;         
        free(freeNode);
        list->len--;
        return;
    }
    struct Node *TravelPoint=list->head;
    while(index > 0)
    {
        TravelPoint =TravelPoint->next;
        index--;
    }
    struct Node *PrevNode=TravelPoint->prev;   //前面的那个指针给到 t的前面那个
    struct Node *NextNode=TravelPoint->next;    //后面的那个指针   给到后面的
    PrevNode->next=TravelPoint->next;           //
    NextNode->prev=TravelPoint->prev;
    free(TravelPoint);
    list->len--;
}

void RemoveByElement(DLlist *list, ElementType element)
{
    int index = FindFirstByElement(list,element);
    while(index !=-1)
    {
        RemoveByIndex(list,index);
        index = FindFirstByElement(list,element);
    }
}

int FindFirstByElement(DLlist *list, ElementType element)
{
    int count = 0;
    struct Node *TravelPoint = list->head;
    while(TravelPoint != NULL)
    {
        if(TravelPoint->data == element)
        {
            return count;
        }
        count++;
        TravelPoint = TravelPoint->next;
    }
    return -1;
}

void FreeDLlist(DLlist *list)
{
    while(list->head != NULL)
    {
        struct Node *freeNode = list->head;
        list->head = list->head->next;
        free(freeNode);
    }
    list->head = NULL;
    list->tail = NULL;
    list->len = 0;
}